<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";

// let dayList = [];
// for (let i = 1; i < 31; i++) {
//   dayList.push(i);
// }
export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "200px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.chartData);
    },

    setOptions(data) {
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
        },
        grid: {
          left: "16",
          top: "30",
          right: "14",
          bottom: "10",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: data.map((item) => {
              return item.date;
            }),
            axisLabel: {
              textStyle: {
                color: "#999",
                fontSize: "12",
              },
            },
            axisLine: {
              lineStyle: {
                color: "#999",
              },
            },
          },
        ],
        yAxis: [
          {
            name: "风险藏品数",
            type: "value",
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "#999",
              },
            },
            axisLabel: {
              textStyle: {
                color: "#999",
                fontSize: "12",
              },
            },
            splitLine: {
              lineStyle: {
                color: "rgba(0,0,0,0.06)",
              },
            },
          },
        ],
        series: [
          {
            name: "新增风险藏品数",
            type: "line",
            smooth: true,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(
                0,
                0,
                0,
                1,
                [
                  {
                    offset: 0,
                    color: "rgba(0, 216, 135, 0.4)",
                  },
                  {
                    offset: 0.8,
                    color: "rgba(0, 216, 135, 0.1)",
                  },
                ],
                false
              ),
              shadowColor: "rgba(0, 0, 0, 0.1)",
            },
            emphasis: {
              focus: "series",
            },
            symbol: "circle",
            symbolSize: 8,
            showSymbol: false,
            itemStyle: {
              color: "#00d887",
              borderColor: "rgba(221, 220, 107, .1)",
              borderWidth: 12,
            },
            data: data.map((item) => {
              return item.num;
            }),
          },
        ],
      });
    },
  },
};
</script>
